/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.integrator.web;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import org.apache.commons.email.SimpleEmail;

        


public class EnviaEmailContato extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher rd;
        DateFormat dateBrFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        
        try {
            //Recupera os valores dos campos da pagina
            String nome = request.getParameter("nome");
            String emailRetorno = request.getParameter("email");
            String telefone = request.getParameter("telefone");
            String cidade = request.getParameter("cidade");
            String assunto = request.getParameter("assunto");
            String mensagem = request.getParameter("textodamensagem");
            String dtHjFormatado = dateBrFormat.format(new Date());
            
            //Texto do e-mail
            String emailMsg = "Contato do(a) Sr(a) " + nome +
                    " em " + dtHjFormatado + "." +
                    "\ne-mail: " + emailRetorno +
                    "\nTelefone: " + telefone +
                    "\nCidade: " + cidade +
                    "\nAssunto: " + assunto +
                    "\nMensagem: " + mensagem;
            
          //  SimpleEmail email = new SimpleEmail();
            //Configuração do Servidor SMTP
           // email.setHostName("smtp.gmail.com"); //Não alterar
           // email.setSmtpPort(25); //Não alterar
          //  email.setSSLOnConnect(true); //Não alterar
          //  email.setAuthentication("fulanodetal", "123456fulan"); //Alterar para o usuário e senha que vai enviar o e-mail
            
            //Gerar e-mail
          //  email.addTo("rsalzo.salzo@gmail.com"); //Alterar para o endereço que vai receber o e-mail
           // email.setFrom("testeTrabalho@fatecpg.com.br");
           // email.setSubject("Contato data " + dtHjFormatado);
         //   email.setMsg(emailMsg);
            
            //Envia o e-mail
          //  email.send();
            
            //Se deu certo, confirma o envio para o usário
            rd = request.getRequestDispatcher("enviado.html");
            rd.forward(request, response);
        } catch( Exception e) {
            //Falha ao enviar o e-mail
            rd = request.getRequestDispatcher("erro.html");
            rd.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

